{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "######1列的索引\n",
    "#1-1通过[]指定特定的列;指定多个列，使用列表\n",
    "#1-2通过使用点号，引用特定的列\n",
    "#1-3使用get函数，针对没有的列取值\n",
    "#1-4对列进行重命名 df.columns=[新列名列表]\n",
    "#####2行的索引\n",
    "#2-1通过[startpos:endpos:step]切片方式\n",
    "#2-2使用loc[]操作符:它允许面向标签(index)的索引.传递多行的列表\n",
    "#2-3使用iloc[]操作符:它允许基于下标的索引,可以使用切片\n",
    "#2-4使用ix[]操作符:它允许混合标签和基于下标的索引，相当于loc和iloc的混合\n",
    "#2-5通过set_index函数设置行索引\n",
    "#####3混合索引定位元素\n",
    "#3-1使用iloc或loc定位到行和列\n",
    "#3-2使用iat、at、ix函数定位特定的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "city_data_dict={'BeiJing' : {'2019-Q1':100000, '2019-Q2':120000, '2019-Q3':150000, '2019-Q4':180000},\n",
    "'ShangHai':{'2019-Q1':120000, '2019-Q2':140000,'2019-Q3':170000,'2019-Q4':190000},\n",
    "'GuangZhou':{'2019-Q1':90000, '2019-Q2':110000, '2019-Q3':160000,'2019-Q4':180000},\n",
    "'ShenZhen':{'2019-Q1':97000, '2019-Q2':112000, '2019-Q3':156000,'2019-Q4':187000}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "city_data=pd.DataFrame.from_dict(city_data_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>BeiJing</th>\n",
       "      <th>GuangZhou</th>\n",
       "      <th>ShangHai</th>\n",
       "      <th>ShenZhen</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-Q1</th>\n",
       "      <td>100000</td>\n",
       "      <td>90000</td>\n",
       "      <td>120000</td>\n",
       "      <td>97000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q2</th>\n",
       "      <td>120000</td>\n",
       "      <td>110000</td>\n",
       "      <td>140000</td>\n",
       "      <td>112000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q3</th>\n",
       "      <td>150000</td>\n",
       "      <td>160000</td>\n",
       "      <td>170000</td>\n",
       "      <td>156000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q4</th>\n",
       "      <td>180000</td>\n",
       "      <td>180000</td>\n",
       "      <td>190000</td>\n",
       "      <td>187000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         BeiJing  GuangZhou  ShangHai  ShenZhen\n",
       "2019-Q1   100000      90000    120000     97000\n",
       "2019-Q2   120000     110000    140000    112000\n",
       "2019-Q3   150000     160000    170000    156000\n",
       "2019-Q4   180000     180000    190000    187000"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([u'2019-Q1', u'2019-Q2', u'2019-Q3', u'2019-Q4'], dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([u'BeiJing', u'GuangZhou', u'ShangHai', u'ShenZhen'], dtype='object')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-Q1    100000\n",
       "2019-Q2    120000\n",
       "2019-Q3    150000\n",
       "2019-Q4    180000\n",
       "Name: BeiJing, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过点号应用特定的列\n",
    "city_data.BeiJing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>BeiJing</th>\n",
       "      <th>ShangHai</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-Q1</th>\n",
       "      <td>100000</td>\n",
       "      <td>120000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q2</th>\n",
       "      <td>120000</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q3</th>\n",
       "      <td>150000</td>\n",
       "      <td>170000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q4</th>\n",
       "      <td>180000</td>\n",
       "      <td>190000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         BeiJing  ShangHai\n",
       "2019-Q1   100000    120000\n",
       "2019-Q2   120000    140000\n",
       "2019-Q3   150000    170000\n",
       "2019-Q4   180000    190000"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1-1通过[]指定特定的列;指定多个列，使用列表\n",
    "city_data[['BeiJing','ShangHai']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-Q1     90000\n",
       "2019-Q2    110000\n",
       "2019-Q3    160000\n",
       "2019-Q4    180000\n",
       "Name: GuangZhou, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data['GuangZhou'] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'N/A'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1-2使用get函数，针对没有的列值，返回空值。如果不使用，则会报错\n",
    "city_data.get('HangZhou','N/A')    #没有杭州的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1-4对列进行重命名 df.columns=[新列名列表]\n",
    "city_data.columns=['北京','上海','广州','深圳']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>上海</th>\n",
       "      <th>广州</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-Q1</th>\n",
       "      <td>100000</td>\n",
       "      <td>90000</td>\n",
       "      <td>120000</td>\n",
       "      <td>97000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q2</th>\n",
       "      <td>120000</td>\n",
       "      <td>110000</td>\n",
       "      <td>140000</td>\n",
       "      <td>112000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q3</th>\n",
       "      <td>150000</td>\n",
       "      <td>160000</td>\n",
       "      <td>170000</td>\n",
       "      <td>156000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q4</th>\n",
       "      <td>180000</td>\n",
       "      <td>180000</td>\n",
       "      <td>190000</td>\n",
       "      <td>187000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             北京      上海      广州      深圳\n",
       "2019-Q1  100000   90000  120000   97000\n",
       "2019-Q2  120000  110000  140000  112000\n",
       "2019-Q3  150000  160000  170000  156000\n",
       "2019-Q4  180000  180000  190000  187000"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>上海</th>\n",
       "      <th>广州</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-Q2</th>\n",
       "      <td>120000</td>\n",
       "      <td>110000</td>\n",
       "      <td>140000</td>\n",
       "      <td>112000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             北京      上海      广州      深圳\n",
       "2019-Q2  120000  110000  140000  112000"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2-1通过[startpos:endpos:step]切片方式查找行\n",
    "city_data[1:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "北京    100000\n",
       "上海     90000\n",
       "广州    120000\n",
       "深圳     97000\n",
       "Name: 2019-Q1, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2-2使用loc[]操作符:它允许面向标签(index)的索引\n",
    "city_data.loc['2019-Q1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>上海</th>\n",
       "      <th>广州</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-Q2</th>\n",
       "      <td>120000</td>\n",
       "      <td>110000</td>\n",
       "      <td>140000</td>\n",
       "      <td>112000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q1</th>\n",
       "      <td>100000</td>\n",
       "      <td>90000</td>\n",
       "      <td>120000</td>\n",
       "      <td>97000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             北京      上海      广州      深圳\n",
       "2019-Q2  120000  110000  140000  112000\n",
       "2019-Q1  100000   90000  120000   97000"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#同样也可以传递多行的列表\n",
    "city_data.loc[['2019-Q2','2019-Q1']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "北京    120000\n",
       "上海    110000\n",
       "广州    140000\n",
       "深圳    112000\n",
       "Name: 2019-Q2, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data.iloc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>上海</th>\n",
       "      <th>广州</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-Q1</th>\n",
       "      <td>100000</td>\n",
       "      <td>90000</td>\n",
       "      <td>120000</td>\n",
       "      <td>97000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q2</th>\n",
       "      <td>120000</td>\n",
       "      <td>110000</td>\n",
       "      <td>140000</td>\n",
       "      <td>112000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             北京      上海      广州      深圳\n",
       "2019-Q1  100000   90000  120000   97000\n",
       "2019-Q2  120000  110000  140000  112000"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2-3使用iloc[]操作符:它允许基于下标的索引,可以使用切片\n",
    "city_data.iloc[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120000"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过行列结合，访问特定的元素\n",
    "city_data.loc['2019-Q2']['北京']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(city_data.loc[['2019-Q2','2019-Q1']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "160000"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过行列结合，访问特定的元素\n",
    "city_data.iloc[2,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "180000"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data.iat[3,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>上海</th>\n",
       "      <th>广州</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-Q1</th>\n",
       "      <td>100000</td>\n",
       "      <td>90000</td>\n",
       "      <td>120000</td>\n",
       "      <td>97000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q2</th>\n",
       "      <td>120000</td>\n",
       "      <td>110000</td>\n",
       "      <td>140000</td>\n",
       "      <td>112000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q3</th>\n",
       "      <td>150000</td>\n",
       "      <td>160000</td>\n",
       "      <td>170000</td>\n",
       "      <td>156000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-Q4</th>\n",
       "      <td>180000</td>\n",
       "      <td>180000</td>\n",
       "      <td>190000</td>\n",
       "      <td>187000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             北京      上海      广州      深圳\n",
       "2019-Q1  100000   90000  120000   97000\n",
       "2019-Q2  120000  110000  140000  112000\n",
       "2019-Q3  150000  160000  170000  156000\n",
       "2019-Q4  180000  180000  190000  187000"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
